home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
03 - Advanced Graphics
/
Example 1
/
sprite.c
< prev
next >
Wrap
Text File
|
1995-02-20
|
3KB
|
131 lines
//
// File: sprite.c
//
// This file contains the routines to draw the sprites
//
// 2/19/95 -- Created by Mick
//
// include files
#include "global.h"
#include "sprite.h"
// defines for this file
// global function declarations
tSpriteInfo *loadSprite( signed short inSpriteResID );
void disposeSprite( tSpriteInfo *inSpriteInfo );
void startSpriteDraw( Rect *inClipRect );
void endSpriteDraw( void );
void drawSprite( tSpriteInfo *inSpriteInfo, Point inWhere );
// global data owned by this file
// local function declarations
// static data
RgnHandle sOldClipRgn; // storage space for the clip region before the startSpriteDraw call
// functions
//
// loadSprite -
//
// Loads/allocates all the data for a sprite.
//
tSpriteInfo *loadSprite( signed short inSpriteResID )
{
tSpriteInfo *newSprite; // the new sprite data
// create the sprite info record
newSprite = ( tSpriteInfo * )NewPtr( sizeof( tSpriteInfo ) );
// load the pict
newSprite->fSpritePict = GetPicture( inSpriteResID );
if ( newSprite->fSpritePict == ( PicHandle )kNil )
{
// if it did not load, drop into the debugger -- real programs would have error checking
Debugger();
}
// copy its bounds rect
newSprite->fSpriteRect = ( *( newSprite->fSpritePict ) )->picFrame;
// return the new sprite!
return newSprite;
}
//
// disposeSprite -
//
// Disposes/releases all the memory used in a sprite
//
void disposeSprite( tSpriteInfo *inSpriteInfo )
{
// dump the picture
ReleaseResource( ( Handle )( inSpriteInfo->fSpritePict ) );
// free the structure
DisposePtr( ( Ptr )inSpriteInfo );
}
//
// startSpriteDraw -
//
// Prepare the sprite draw. Assumes that the port is set to the destination port.
//
void startSpriteDraw( Rect *inClipRect )
{
// save the current port's clip region
sOldClipRgn = NewRgn();
GetClip( sOldClipRgn );
// set the clip region to be the passed in rect
ClipRect( inClipRect );
}
//
// endSpriteDraw -
//
// End the sprite draw sequence.
//
void endSpriteDraw( void )
{
// restore the old clip region
SetClip( sOldClipRgn );
// dump the region
DisposeRgn( sOldClipRgn );
}
//
// drawSprite -
//
// Draw the sprite in the port.
//
void drawSprite( tSpriteInfo *inSpriteInfo, Point inWhere )
{
Rect destRect; // where we want to draw the sprite
// calculate the destination rect
destRect = inSpriteInfo->fSpriteRect;
OffsetRect( &destRect, inWhere.h, inWhere.v );
// draw the sprite
DrawPicture( inSpriteInfo->fSpritePict, &destRect );
}